package test2.tx;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class App2 {
	
	static String url = "jdbc:mysql://localhost/demo?characterEncoding=UTF-8";
	static String user = "root";
	static String password = "";
	
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, user, password);
			conn.setAutoCommit(false);
//			conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
//			conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
//			conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
			conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			
			ps = conn.prepareStatement("select money from test");
			rs = ps.executeQuery();
			while(rs.next()) {
				System.out.println(rs.getInt("money"));
			}
			
			
			Thread.sleep(5000);
			
			ps = conn.prepareStatement("update test set money = 2000");
			int num = ps.executeUpdate();
			System.out.println("影响了" + num + "行");
			ps = conn.prepareStatement("select money from test");
			rs = ps.executeQuery();
			while(rs.next()) {
				System.out.println(rs.getInt("money"));
			}
			conn.commit();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(ps!=null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
